
local function GetStringsTable(inst)
    return TUNING.NPC_CHAT_TABLE[ inst.npc_base_lib:Get_Language() ] ["npc_event_fishbowl"] or {}
end



local function food_accept_test(inst,item)
    if inst.components.inventoryitem.owner ~= nil then
        return false
    end

    if item.prefab == "npc_item_fishbowl" then
        return true
    end

    -- item:HasTag("preparedfood")
    if not item:HasTag("preparedfood") then
        return false
    end

    if item.components.edible == nil then
        return false
    end

    return true
end
local function OnGetItem(inst, giver, item)
    local pt = Vector3(inst.Transform:GetWorldPosition())
    local gold_trans_point = 25 --- 25 点换1金
    local gold_num_max = 150 ---- 最多给多少金
    if TUNING.NPC_CONFIG.REWARDS_LESS == true then
        gold_trans_point = 35
    end
    -----------------------------------------------------------------------------------------------------------------------------------
    ---- 如果是鱼缸喂鱼缸里
    if item.prefab == "npc_item_fishbowl" then
        SpawnPrefab("npc_fx_waterspout"):PushEvent("Set",{
            pt = pt,
            color = Vector3(255,255,0),
            scale = Vector3(2.5,2.5,2.5),
        })

        local num = 50
        local stack_max = 15
        if TUNING.NPC_CONFIG.REWARDS_LESS == true then
            num = math.random(30, 50)
            stack_max = 10
        end

        giver:DoTaskInTime(1.5,function()
            for i = 1, num, 1 do
                local r_pt = giver.npc_base_lib:GetSpawnPoint(pt,math.random(3,6)) or pt
                local gold = SpawnPrefab("goldnugget")
                gold.Transform:SetPosition(r_pt.x,math.random(15,30),r_pt.z)
                gold.components.stackable.stacksize = math.random(stack_max) -- gold.components.stackable.maxsize -- maxsize default = 20
            end
        end)

        giver:DoTaskInTime(2,function()
            giver.npc_base_lib:Wisper(GetStringsTable(giver).fishbowl_for_food,true)            
        end)

        inst:Remove()
        return
    end

    -----------------------------------------------------------------------------------------------------------------------------------


    ---------- 取最大的值
    local max_value = item.components.edible.healthvalue
    if item.components.edible.hungervalue > max_value then
        max_value = item.components.edible.hungervalue
    end
    if item.components.edible.sanityvalue > max_value then
        max_value = item.components.edible.sanityvalue
    end
    if max_value < 0 then
        max_value = 10
    end
    -----------------------------------------------------

    -- print("Food max",inst.npc_everything_data:Add("food_max",0))

    if inst.npc_everything_data:Add("food_max",max_value) > gold_trans_point * gold_num_max then 
        ------- 喂了太多，变鲨鱼
        inst:Remove()

        SpawnPrefab("npc_fx_waterspout"):PushEvent("Set",{
            pt = pt,
            color = Vector3(255,34,34),
            scale = Vector3(2.5,2.5,2.5),
        })
        local shark = SpawnPrefab("shark")
        shark.Transform:SetPosition(pt.x,pt.y,pt.z)
        shark.components.combat:SetDefaultDamage(100000)
        
        shark:DoTaskInTime(60,function() shark.components.combat:SetDefaultDamage(TUNING.SHARK.DAMAGE)   end)
        giver:DoTaskInTime(7,function()
            giver.npc_base_lib:Wisper(GetStringsTable(giver).shark_announce)            
        end)
    else    -------------- 没变鲨鱼的时候
        local current_value = inst.npc_everything_data:Add("food_value_rest",max_value)
        -- print("Fishbowl current_value : ",current_value)
        -- print("Fishbowl food_max : ",inst.npc_everything_data:Get("food_max"))

        local gold_num = math.floor( current_value / gold_trans_point )
        local res_value = current_value
        if gold_num ~= 0 then
            res_value = current_value % gold_num    ----  % 0 会出错
        end

        inst.npc_everything_data:Set("food_value_rest",res_value)

        if gold_num > 0 then
            SpawnPrefab("npc_fx_waterspout"):PushEvent("Set",{
                pt = pt,
                color = Vector3(255,255,0)
            })
            inst:DoTaskInTime(1.5,function()
                for i = 1, gold_num, 1 do
                    local r_pt = inst.npc_base_lib:GetSpawnPoint(pt,math.random(3,6)) or pt
                    local gold = SpawnPrefab("goldnugget")
                    gold.Transform:SetPosition(r_pt.x,math.random(15,30),r_pt.z)
                end
            end)
            
        end

    end
    -----------------------------------------------------------------------------------------------------------------------------------
end


local function fn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddNetwork()
    inst.entity:AddSoundEmitter()
    MakeInventoryPhysics(inst)

    -- inst.Transform:SetTwoFaced() --- 多面体效果并不好

    inst.AnimState:SetBank("fishbowl")
    inst.AnimState:SetBuild("hutch_fishbowl")
    inst.AnimState:PlayAnimation("idle_loop",true)


    inst:AddTag("trader")
    inst:AddTag("alltrader")
    inst:AddTag("fishbowl")
    inst:AddTag("fishbowl_ex")
    inst:AddTag("smallcreature")



    inst.talk_sound = "hookline_2/characters/hermit/talk"
    ---------------------------------------------------------------------------------------------
    -- talker 组件   net_bool 帮助处理偏移
    -- local TALLER_TALKER_OFFSET = Vector3(0, -700, 0)
    local colours = {
        Vector3( 255 /255, 193 /255 , 37 /255),     -- Goldenrod1 橙黄色
        Vector3( 127 /255, 255 /255 , 0 /255),      --- Chartreuse 青绿色
        Vector3( 15 /255, 15 /255 , 15 /255),      --- black 黑色
        Vector3( 255 /255, 69 /255 , 0 /255),       -- OrangeRed 橘红色
        Vector3( 0 /255, 191 /255 , 255 /255),      -- DeepSkyBlue 天蓝色
        Vector3( 255 /255, 255 /255 , 0 /255),      -- yellow 黄色
        Vector3( 34 /255, 139 /255 , 34 /255),      -- ForestGreen 深绿色
        Vector3( 238 /255, 233 /255 , 233 /255),     -- Snow2  雪白色
    }

    inst:AddComponent("talker")
    inst.components.talker.fontsize = 35
    inst.components.talker.font = TALKINGFONT
    inst.components.talker.color_num = 1
    inst.components.talker.colour = colours[inst.components.talker.color_num]
    inst.components.talker.offset = Vector3(0, -230, 0)
    inst.components.talker.Say_old_batbat = inst.components.talker.Say
    inst.components.talker.Say = function(self,...)
        self:Say_old_batbat(...)
        self.color_num = self.color_num + 1
        if self.color_num > #colours then
            self.color_num = 1
        end
        self.colour = colours[self.color_num]
    end
    ---------------------------------------------------------------------------------------------





    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end
    inst:AddComponent("npc_base_lib")
    inst:AddComponent("npc_everything_data")


    inst:AddComponent("named")
    inst:DoTaskInTime(0,function()   
        if inst.components.named.name == nil then     
            inst.components.named.possiblenames = inst.npc_base_lib:Get_Names()
            inst.components.named:PickNewName()
        end
    end)


    inst:AddComponent("inspectable")
    STRINGS.CHARACTERS.GENERIC.DESCRIBE[string.upper("npc_item_fishbowl")] = GetStringsTable(inst).inspect_str  --人物检查的描述

    inst:AddComponent("inventoryitem")
    inst.fishAlive = "hutch_fishbowl"
    inst.fishDead = "hutch_fishbowl_dead"
    inst.components.inventoryitem:ChangeImageName(inst.fishAlive)

    -----------------------------------------------------------------------------------
    -- local function ShouldAcceptItem(inst, item)
    --     if item and item.prefab == "reviver" then
    --         return true
    --     end
    -- end
    -- local function OnGetItem(inst, giver, item)

    -- end
    -- local function abletoaccepttest(inst,item,giver)
    --     if item and item.prefab == "reviver" then
    --         return true
    --     end
    -- end

    inst:AddComponent("trader")
    inst.components.trader:SetAcceptTest(food_accept_test)
    inst.components.trader.onaccept = OnGetItem
    inst.components.trader.deleteitemonaccept = true
    inst.components.trader:SetAbleToAcceptTest(food_accept_test)

    inst:AddComponent("tradable") ---- 可以被交易出去
    -----------------------------------------------------------------------------------
    ---- 动态定时检查
    local function ask_for_food_task(inst)
        if not inst:IsValid() then
            return
        end

        local player_near_flag = inst:IsNearPlayer(15,true)
        if inst.components.inventoryitem.owner == nil and player_near_flag == true then
            inst.ask_for_food_task_time = math.random(5,15)
            local chat_table = GetStringsTable(inst).ask_for_food
            inst.npc_base_lib:Say(chat_table,true)
            inst.ask_for_food_task_time = math.random(5,10)
        else
            if player_near_flag == true then
                inst.ask_for_food_task_time = 6
            else
                inst.ask_for_food_task_time = 20
            end
        end
        inst:DoTaskInTime(inst.ask_for_food_task_time,ask_for_food_task)
    end
    inst.ask_for_food_task_time = 2
    inst:DoTaskInTime(inst.ask_for_food_task_time,function()
        ask_for_food_task(inst)
    end)

    -----------------------------------------------------------------------------------

    MakeHauntableLaunch(inst)
    return inst
end


return Prefab("npc_item_fishbowl", fn)